import 'package:flutter/material.dart';
import 'package:rx_dart_app/page/tab/test_page.dart';

import '../money/index.dart';

class TabPageView extends StatefulWidget {
  const TabPageView({Key? key}) : super(key: key);

  @override
  State<TabPageView> createState() => _TabPageViewState();
}

class _TabPageViewState extends State<TabPageView> {
  List<Widget> _pages = [];
  List<BottomNavigationBarItem> _items = [];
  int _currentIndex = 0; // 当前索引
//第一步：创建PageController 并制定初始选中页面
  final PageController _controller = PageController(initialPage: 0); // 页面控制器

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initPages();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        physics: const NeverScrollableScrollPhysics(),
        controller: _controller,
        children: _pages,
      ),
      bottomNavigationBar: BottomNavigationBar(
        fixedColor: Colors.blueAccent,
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          _controller.jumpToPage(index);
          setState(() {
            _currentIndex = index;
          });
        },
        items: _items,
      ),
    );
  }

  initPages() {
    _pages = [
      const MoneyPage(
        content: "记账",
      ),
      const TestPage(content: "好友"),
      const TestPage(content: "添加"),
      const TestPage(content: "发现"),
      const TestPage(content: "我的")
    ];
    _items = [
      _bottomItem(
          const Icon(Icons.home), const Icon(Icons.home_filled), "记账", 0),
      _bottomItem(
          const Icon(Icons.home), const Icon(Icons.home_filled), "好友", 1),
      _bottomItem(
          const Icon(Icons.home), const Icon(Icons.home_filled), "添加", 2),
      _bottomItem(
          const Icon(Icons.home), const Icon(Icons.home_filled), "发现", 3),
      _bottomItem(
          const Icon(Icons.home), const Icon(Icons.home_filled), "我的", 4),
    ];
  }

  // 底部导航item
  BottomNavigationBarItem _bottomItem(
      Widget icon, Widget activeIcon, String label, int index) {
    return BottomNavigationBarItem(
      icon: icon,
      activeIcon: activeIcon,
      label: label,
    );
  }
}
